home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickTime Conferencing / Programming Stuff / Sample Code / SeeWorld / BigBrother.c next >
Encoding:
C/C++ Source or Header  |  1995-04-23  |  8.3 KB  |  469 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        BigBrother.c
  3.  
  4.     Contains:    A Conferencing server with trivial security
  5.  
  6.     Written by:    Guy Riddle
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. #include <Types.h>
  13. #include <QuickDraw.h>
  14. #include <Fonts.h>
  15. #include <Events.h>
  16. #include <Windows.h>
  17. #include <Menus.h>
  18. #include <Dialogs.h>
  19. #include <Desk.h>
  20. #include <ToolUtils.h>
  21. #include <Memory.h>
  22. #include <SegLoad.h>
  23. #include <Files.h>
  24. #include <Folders.h>
  25. #include <OSUtils.h>
  26. #include <DiskInit.h>
  27. #include <Scrap.h>
  28.  
  29. #include <String.h>
  30. #include <Strings.h>
  31. #include <stdio.h>
  32.  
  33. #include <QuickTimeConferencing.h>
  34.  
  35. #include "See.h"
  36.  
  37. typedef    EventRecord            *EventRecordPtr;
  38. typedef    Byte                *BytePtr;
  39.  
  40. #define        MAXMEMBERS        0
  41.  
  42. typedef struct {
  43.     Rect                    box;
  44.     MTConferenceMember        member;
  45.     MTControllerComponent    controller;
  46. } Member, *MemberPtr;
  47.  
  48. #define        WRECT(top, left)    (top),(left),(top+120),(left+160)
  49.  
  50.     Member                    gMember[MAXMEMBERS+1]    = {
  51.                                 {    WRECT(100, 100)        },
  52.                             };
  53.  
  54. #define        PW_CLEAR            'pw-C'                            
  55.                             
  56.     MTCapabilitiesList        gCapabilities            = { 1, { PW_CLEAR, 1, mtMessageRequiredCapability } };
  57.                             
  58.     MTCString63                gUserName, gPassword;
  59.     
  60.     ComponentInstance        gCC;
  61.     MTConferenceToken        gConference;
  62.     
  63.     void            Initialize(void);
  64.     Boolean            Configure(void);
  65.     void            EnableButton(DialogRef, short, Boolean);
  66.     void            GetValue(BytePtr, DialogRef, short);
  67.     void            StartMovieTalk(void);
  68.     void            StopMovieTalk(void);
  69.     void            EventLoop(void);
  70.     
  71.     void            DoToolboxEvent(EventRecordPtr);
  72.     void            DoKeyDownEvent(EventRecordPtr);
  73.     void            DoMouseDownEvent(EventRecordPtr);
  74.     void            DoDiskEvent(EventRecordPtr);
  75.     void            DoMenuBar(long);
  76.     void            DoAppleMenu(short);
  77.     void            DoFileMenu(short);
  78.     
  79.     void            DoConferenceEvent(MTConferenceEventPtr);
  80.     void            DoConfReady(MTConferenceEventPtr);
  81.     void            DoMemberReady(MTConferenceEventPtr);
  82.     void            DoMessageArrived(MTConferenceEventPtr);
  83.     void            DoConfTerminated(MTConferenceEventPtr);
  84.     void            DoIncomingCall(MTConferenceEventPtr);
  85.  
  86.     void            Sorry(int, int, MTConferenceMember, OSErr);
  87.     
  88. /*
  89.  *    Seerver
  90.  *
  91.  *    This is a Server-only version of conferencing -- it doesn't place calls.
  92.  *
  93.  *    • You can configure the name, service type, and optional password
  94.  *    for access
  95.  *
  96.  *    • As a server, it needs no recording or snapshot support
  97.  *
  98.  *    • The password protocol illustrates MessageCapabilities and sending
  99.  *    user messages
  100.  *
  101.  *    • MTConferenceSetMode is used to indicate it doesn't want to receive data
  102.  *
  103.  *    • MTConferenceDetachMember is used to reduce memory overhead when multicast
  104.  *    channels are available
  105.  */
  106.  
  107. void main()
  108. {
  109.     Initialize();
  110.     
  111.     if(Configure()){
  112.         StartMovieTalk();
  113.         EventLoop();
  114.     }
  115. }
  116.  
  117.     void 
  118. Initialize(
  119.     void
  120. ){
  121.     Handle                    menuBar;
  122.  
  123.     InitGraf((Ptr) &qd.thePort);
  124.     InitFonts();
  125.     InitWindows();
  126.     InitMenus();
  127.     TEInit();
  128.     InitDialogs(nil);
  129.     InitCursor();
  130.  
  131.     menuBar = GetNewMBar(rBigBroMenuBar);
  132.  
  133.     SetMenuBar(menuBar);
  134.     DisposHandle(menuBar);
  135.     AddResMenu(GetMHandle(mApple), 'DRVR');
  136.  
  137.     DrawMenuBar();
  138. }
  139.  
  140.     Boolean
  141. Configure(
  142.     void
  143. ){
  144.     DialogRef                dp;
  145.     short                    itemHit;
  146.     
  147.     dp = GetNewDialog(rConfigure, 0, (WindowRef) -1);
  148.     SetDialogCancelItem(dp, diCancel);
  149.     SetDialogDefaultItem(dp, diOK);
  150.     SetDialogTracksCursor(dp, true);
  151.     PostEvent(keyDown, '\b');        // ick
  152.     
  153.     do{
  154.         EnableButton(dp, diOK, gUserName[0]);
  155.         
  156.         ModalDialog(0, &itemHit);
  157.         
  158.         GetValue(gUserName, dp, diName);
  159.         GetValue(gPassword, dp, diPassword);
  160.     }while(itemHit > diCancel);
  161.     
  162.     DisposeDialog(dp);
  163.     InitCursor();
  164.     
  165.     return(itemHit == diOK);
  166. }
  167.  
  168.     void
  169. EnableButton(
  170.     DialogRef                dp,
  171.     short                    item,
  172.     Boolean                    enabled
  173. ){
  174.     Handle                    ih;
  175.     Rect                    rect;
  176.     short                    type;
  177.     
  178.     GetDialogItem(dp, item, &type, &ih, &rect);
  179.  
  180.     if(enabled)
  181.         type &= ~itemDisable;
  182.     else
  183.         type |= itemDisable;
  184.     
  185.     HiliteControl((ControlHandle) ih, enabled ? 0 : 255);
  186.     
  187.     SetDialogItem(dp, item, type, ih, &rect);
  188.     
  189.     SetPort(dp);
  190.     InsetRect(&rect, -5, -5);
  191.     InvalRect(&rect);
  192. }
  193.  
  194.     void
  195. GetValue(
  196.     BytePtr                    p,
  197.     DialogRef                dp,
  198.     short                    item
  199. ){
  200.     Handle                    ih;
  201.     Rect                    rect;
  202.     short                    type;
  203.     
  204.     GetDialogItem(dp, item, &type, &ih, &rect);
  205.     GetDialogItemText(ih, p);
  206.     
  207.     if(p[0] && p[p[0]] < ' '){
  208.         p[0]--;            // strip CR or other junk
  209.         SetDialogItemText(ih, p);
  210.     }
  211.     
  212.     p2cstr(p);
  213. }
  214.  
  215.     void
  216. StartMovieTalk(
  217.     void
  218. ){
  219.     MTCapabilitiesHandle    ch;
  220.     
  221.     gCC = OpenDefaultComponent(kMTConferenceType, kMTMovieTalkSubType);
  222.     
  223.     MTConferenceSetMode(gCC, mtSendMediaModeMask+mtShareableModeMask);
  224.     
  225.     gMember[0].controller = MTConferenceNewPreparedController(gCC, &gMember[0].box,
  226.                               mtMediaSourcePrepMask+mtGrabVideoPrepMask+mtGrabAudioPrepMask+mtWindowVisiblePrepMask+mtControllerVisiblePrepMask,
  227.                               gUserName);
  228.                               
  229.     MTDirectorSetSoundThreshold(MTControllerGetStreamDirector(gMember[0].controller), kMTAllStreams, 0);        // rather not have VOX
  230.                               
  231.     if(gPassword[0]){
  232.         PtrToHand((Ptr) &gCapabilities, (Handle *) &ch, sizeof(gCapabilities));
  233.         MTConferenceSetMessageCapabilities(gCC, ch);
  234.     }
  235.     
  236.     MTConferenceListen(gCC, gUserName, gUserName, (MTCString)"mtlkatlk\tMulticaster\x0Dmtlktcpi\t545\x0Dmtlkisdn\t-\x0D");
  237. }                            
  238.  
  239.     void
  240. EventLoop(
  241.     void
  242. ){
  243.     EventRecord                tEvent;
  244.     MTConferenceEvent        cEvent;
  245.  
  246.     for(;;){
  247.         if(MTConferenceGetNextEvent(gCC, &cEvent))
  248.             DoConferenceEvent(&cEvent);
  249.             
  250.         if(WaitNextEvent(everyEvent, &tEvent, 1, 0))
  251.             DoToolboxEvent(&tEvent);
  252.     }
  253. }
  254.  
  255.     void
  256. DoToolboxEvent(
  257.     EventRecordPtr            ev
  258. ){
  259.     switch(ev->what) {
  260.     case mouseDown:
  261.         DoMouseDownEvent(ev);
  262.         break;
  263.     
  264.     case keyDown:
  265.         DoKeyDownEvent(ev);
  266.         break;
  267.     
  268.     case diskEvt:
  269.         DoDiskEvent(ev);
  270.     }
  271. }
  272.  
  273.     void
  274. DoMouseDownEvent(
  275.     EventRecordPtr            ev
  276. ){
  277.     WindowPtr                wp;
  278.     short                    part;
  279.     
  280.     part = FindWindow(ev->where, &wp);
  281.     
  282.     switch(part){
  283.     case inMenuBar:
  284.         DoMenuBar(MenuSelect(ev->where));
  285.     }
  286. }
  287.  
  288.     void
  289. DoKeyDownEvent(
  290.     EventRecordPtr            ev
  291. ){
  292.     if(ev->modifiers & cmdKey)
  293.         DoMenuBar(MenuKey(ev->message & charCodeMask));
  294. }
  295.  
  296.     void
  297. DoDiskEvent(
  298.     EventRecordPtr            ev
  299. ){
  300.     Point                    where;
  301.  
  302.     if(HiWord(ev->message)){
  303.         SetPt(&where, kDILeft, kDITop);
  304.         DIBadMount(where, ev->message);
  305.     }
  306. }
  307.  
  308.     void
  309. DoMenuBar(
  310.     long                    selection
  311. ){
  312.     short                    menuID;    
  313.     short                    menuItem;
  314.  
  315.     menuID = HiWord(selection);
  316.     menuItem = LoWord(selection);
  317.     
  318.     switch(menuID){
  319.     case mApple:
  320.         DoAppleMenu(menuItem);
  321.         break;
  322.         
  323.     case mFile:
  324.         DoFileMenu(menuItem);
  325.         break;
  326.         
  327.     case mEdit:                    /* call SystemEdit for DA editing & MultiFinder */
  328.         SystemEdit(menuItem-1);
  329.     }
  330.     
  331.     HiliteMenu(0);
  332. }
  333.  
  334.     void
  335. DoAppleMenu(
  336.     short                    item
  337. ){
  338.     DialogRef                dp;
  339.     Str255                    daName;
  340.     short                    itemHit;
  341.  
  342.     switch(item){
  343.     case iAbout:
  344.         dp = GetNewDialog(rAboutAlert, nil, (WindowRef) -1);
  345.         SetDialogDefaultItem(dp, 1);
  346.         
  347.         ModalDialog(0, &itemHit);
  348.         
  349.         DisposeDialog(dp);
  350.         break;
  351.         
  352.     default:            /* all non-About items in this menu are DAs */
  353.         GetItem(GetMHandle(mApple), item, daName);
  354.         OpenDeskAcc(daName);
  355.     }
  356. }
  357.  
  358.     void
  359. DoFileMenu(
  360.     short                    item
  361. ){
  362.     switch(item){
  363.     case iQuit:
  364.         StopMovieTalk();
  365.         
  366.         ExitToShell();
  367.     }
  368. }
  369.  
  370.     void
  371. StopMovieTalk(
  372.     void
  373. ){
  374.     CloseComponent(gCC);
  375. }
  376.  
  377.     void
  378. DoConferenceEvent(
  379.     MTConferenceEventPtr    ce
  380. ){
  381.     switch(ce->what){
  382.     case mtIncomingCallEvent:
  383.         DoIncomingCall(ce);
  384.         break;
  385.  
  386.     case mtConferenceReadyEvent:
  387.         DoConfReady(ce);
  388.         break;
  389.     
  390.     case mtConferenceTerminatedEvent:
  391.         DoConfTerminated(ce);
  392.         break;
  393.     
  394.     case mtMemberReadyEvent:
  395.         DoMemberReady(ce);
  396.         break;
  397.     
  398.     case mtMessageArrivedEvent:
  399.         DoMessageArrived(ce);
  400.         break;
  401.     
  402.     case mtFailedEvent:
  403.         Sorry(701, 2, ce->who, ce->err);
  404.         break;
  405.     }
  406.     
  407.     if(ce->surprise)
  408.         DisposeHandle(ce->surprise);
  409. }
  410.  
  411.     void
  412. DoIncomingCall(
  413.     MTConferenceEventPtr    ce
  414. ){
  415.     if(!gPassword[0])    
  416.         MTConferenceReply(gCC, ce->who, 0);
  417. }
  418.  
  419.     void
  420. DoConfReady(
  421.     MTConferenceEventPtr    ce
  422. ){
  423.     if(gConference)
  424.         MTConferenceMerge(gCC, gConference, ce->who);
  425.     else
  426.         MTConferenceActivateConference(gCC, gConference = ce->who, gMember[0].controller);
  427. }
  428.  
  429.     void
  430. DoMemberReady(
  431.     MTConferenceEventPtr    ce
  432. ){
  433.     MTConferenceActivateMember(gCC, ce->who, 0);
  434.     MTConferenceDetachMember(gCC, ce->who);
  435. }
  436.  
  437.     void
  438. DoMessageArrived(
  439.     MTConferenceEventPtr    ce
  440. ){
  441.     MTConferenceReply(gCC, ce->who, strcmp((char*)gPassword, *ce->surprise) ? afpUserNotAuth : 0);
  442. }
  443.  
  444.     void
  445. DoConfTerminated(
  446.     MTConferenceEventPtr    ce
  447. ){
  448.     if(gConference == ce->who)
  449.         gConference = 0;
  450. }
  451.  
  452.     void
  453. Sorry(
  454.     int                        why,
  455.     int                        note,
  456.     MTConferenceMember            who,
  457.     OSErr                    err
  458. ){
  459.     Str255                p1, p2, p3;
  460.     
  461.     GetIndString(p1, why, note);
  462.     
  463.     strcpy((char*)p2, (char*)MTConferenceGetMemberName(gCC, who));
  464.     sprintf((char*)p3, "%d", err);
  465.     
  466.     ParamText(nil, p1, c2pstr((char*)p2), c2pstr((char*)p3));
  467.     
  468.     CautionAlert(why, 0);
  469. }